#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：handler.py
@Author  ：赵加响
@Date    ：2023/5/10 15:58 
@Desc    ：
"""
import os
import time

from constants import LOG_PATH
from utils.utils import create_folder

timestamp = time.localtime(time.time())
# 获取本地时间
rq = time.strftime("%Y%m%d", timestamp)
# 设置日志存放路径
log_path = os.path.join(LOG_PATH, time.strftime('%Y-%m', timestamp))
debug_log_path = os.path.join(LOG_PATH, time.strftime('debug-%Y-%m', timestamp))
# 如果日志目录不存在，就自动创建
create_folder(log_path)
create_folder(debug_log_path)
# 设置日志文件名
log_name = os.path.join(log_path, rq + ".log")
debug_log_name = os.path.join(debug_log_path, rq + ".log")

LOGGING_SETTING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "{levelname} {asctime} {module} {process:d} {thread:d} {message}",
            "style": "{",
        },
        "simple": {
            "format": "{levelname} {message}",
            "style": "{",
        },
    },
    'handlers': {
        'file': {
            'filename': log_name,
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'when': 'midnight',
            'interval': 1,
            'backupCount': 7,
            'formatter': 'verbose',
            'encoding': 'utf-8',
        },
        'debug_file': {
            'filename': debug_log_name,
            'level': 'DEBUG',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'when': 'midnight',
            'formatter': 'verbose',
            'encoding': 'utf-8',
        },
        "console": {
            "class": "logging.StreamHandler",
            'level': 'INFO',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
        },
        "django.request": {
            "handlers": ['file'],
            "level": "INFO",
            "propagate": False,
        },
        "django.debug": {
            "handlers": ['debug_file'],
            "level": "DEBUG",
            "propagate": False,
        },
    }
}
